package com.gdth.bank.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.bank.bean.SynchronizeBean;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.DzCpkhXxDjPos;
import com.gdth.database.bean.DzCpkhXxDjPosDao;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.database.bean.TAppFjxxbDao;
import com.gdth.http.method.HttpMethodServlet;
import com.gdth.utils.GsonUtil;
import com.gdth.utils.StringUtils;

import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.QueryBuilder;


public class DzCpKhXxPosModel {
	public Observable<List<DzCpkhXxDjPos>> saveDzcpkhxxpos(final DzCpkhXxDjPos mDzCpkhXxDjPos){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjPos>>() {

			@Override
			public void call(final Subscriber<? super List<DzCpkhXxDjPos>> arg0) {
				// TODO Auto-generated method stub
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						
						boolean re = true;
						Exception ex = null;
						
						DzCpkhXxDjPosDao dao = session.getDzCpkhXxDjPosDao();
						String wybs = mDzCpkhXxDjPos.getAppUuid();
						try{
							dao.insertOrReplace(mDzCpkhXxDjPos);
							Long id = mDzCpkhXxDjPos.getId();
							//System.out.println(id);
							if (mDzCpkhXxDjPos.getFjxxs() != null && mDzCpkhXxDjPos.getFjxxs().size() > 0) {
								TAppFjxxbDao fjxxDao = session.getTAppFjxxbDao();
								QueryBuilder<TAppFjxxb> fjxxqb = fjxxDao.queryBuilder();
								fjxxqb.where(TAppFjxxbDao.Properties.Wybs.eq(wybs));
								DeleteQuery<TAppFjxxb> deletefjxx = fjxxqb.buildDelete();
								deletefjxx.executeDeleteWithoutDetachingEntities();
								fjxxDao.insertOrReplaceInTx(mDzCpkhXxDjPos.getFjxxs());
							}
//							TAppFjxxbDao fjxxDao = session.getTAppFjxxbDao();
//							QueryBuilder<TAppFjxxb> fjxxqb = fjxxDao.queryBuilder();
//							fjxxqb.where(TAppFjxxbDao.Properties.Wybs.eq(wybs));
//							DeleteQuery<TAppFjxxb> deletefjxx = fjxxqb.buildDelete();
//							deletefjxx.executeDeleteWithoutDetachingEntities();
//							fjxxDao.insertOrReplaceInTx(mDzCpkhXxDjPos.getFjxxs());
						}catch(Exception e){
							ex = e;
							re = false;
						}
						List<DzCpkhXxDjPos> mList = new ArrayList<DzCpkhXxDjPos>();
						mList.add(mDzCpkhXxDjPos);
						if (re){
							arg0.onNext(mList);
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}
	public Observable<List<DzCpkhXxDjPos>> getAllDzcpkhxxpos(final String val){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjPos>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjPos>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjPosDao dao = BankApplication.getDaoSession().getDzCpkhXxDjPosDao();
				QueryBuilder<DzCpkhXxDjPos> qb = dao.queryBuilder();
				if (val != null && !val.equals(""))
					if(StringUtils.isNumeric(val)==true){
						qb.where(DzCpkhXxDjPosDao.Properties.KhZjh.like("%"+val+"%"));
					}else{
						qb.where(DzCpkhXxDjPosDao.Properties.KhMc.like("%"+val+"%"));
					}
					
				qb.where(DzCpkhXxDjPosDao.Properties.Xgzt.eq("0"));
				List<DzCpkhXxDjPos> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			 
			}
		});
	}
	public Observable<List<DzCpkhXxDjPos>> getDzcpkhxxpos(final String uuid){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjPos>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjPos>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjPosDao dao = BankApplication.getDaoSession().getDzCpkhXxDjPosDao();
				QueryBuilder<DzCpkhXxDjPos> qb = dao.queryBuilder();
				if (uuid != null && !uuid.equals(""))
					qb.where(DzCpkhXxDjPosDao.Properties.AppUuid.eq(uuid));

				TAppFjxxbDao fjDao= BankApplication.getDaoSession().getTAppFjxxbDao();
				QueryBuilder<TAppFjxxb> fjqb=fjDao.queryBuilder();
				fjqb.where(TAppFjxxbDao.Properties.Wybs.eq(uuid));
				List<TAppFjxxb> fjList = fjqb.list();
				
				List<DzCpkhXxDjPos> mList = qb.list();
				DzCpkhXxDjPos mDzCpkhXxDjPos = new DzCpkhXxDjPos();
				if(mList.size()==1){
					mDzCpkhXxDjPos =mList.get(0);
					
				}
				if(fjList.size()>0){
					mDzCpkhXxDjPos.setFjxxs(fjList);
//					mDzCpkhXxDjPos.getFjxxs().addAll(fjList);
				}
				/*DzCpkhXxDjPos  mDzCpkhXxDjPos= mList.get(0);
				if(fjList.size()>0){
					mDzCpkhXxDjPos.setFjxxs(fjList);
				}*/
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			 
			}
		});
	}
	public Observable<List<DzCpkhXxDjPos>> deleteDzcpkhxxpos(final String uuid){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjPos>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjPos>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjPosDao dao = BankApplication.getDaoSession().getDzCpkhXxDjPosDao();
				QueryBuilder<DzCpkhXxDjPos> qb = dao.queryBuilder();
				if (uuid != null && !uuid.equals(""))
					qb.where(DzCpkhXxDjPosDao.Properties.AppUuid.eq(uuid));
				
				List<DzCpkhXxDjPos> mList = qb.list();
				if(mList.size()==1){
					dao.delete(mList.get(0));
				}
			 
			}
		});
    }
	public Observable<List<DzCpkhXxDjPos>> getAllDzcpkhxxposList(final int offset , final String time){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjPos>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjPos>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjPosDao dao = BankApplication.getDaoSession().getDzCpkhXxDjPosDao();
				/*QueryBuilder<DzCpkhXxDjEtc> qb = dao.queryBuilder();
				qb.orderDesc(Properties.Sqsj);*/
				QueryBuilder<DzCpkhXxDjPos> qb = dao.queryBuilder().orderDesc(DzCpkhXxDjPosDao.Properties.SqSj);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = null;
				if (offset == 0){
					date = new Date();
				} else {
					try {
						date=sdf.parse(time);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				try {
					qb.where(DzCpkhXxDjPosDao.Properties.SqSj.between(sdf.parse("2016-07-29 00:00:00"),date));
					List<DzCpkhXxDjPos> mList = qb.list();
					qb.offset(0 * 10).limit(10);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				List<DzCpkhXxDjPos> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			 
			}
		});
	}
	
	public Observable<List<TAppFjxxb>> getFjXx(final String uuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppFjxxb>>() {

			@Override
			public void call(Subscriber<? super List<TAppFjxxb>> arg0) {
				// TODO Auto-generated method stub
				TAppFjxxbDao fjDao= BankApplication.getDaoSession().getTAppFjxxbDao();
				QueryBuilder<TAppFjxxb> fjqb=fjDao.queryBuilder();
				fjqb.where(TAppFjxxbDao.Properties.Wybs.eq(uuid));
				List<TAppFjxxb> fjList = fjqb.list();
				if (fjList == null){
					arg0.onError(null);
				} else {
					
					arg0.onNext(fjList);
					arg0.onCompleted();
				}
				
				
			}	
		});
	}
	/**
	 * pos上传状态
	 * @param arg0
	 */
	public Observable<String> updateDzcpkhxxpos(String content){
		return HttpMethodServlet.getInstance().upLoadDzcpPos(content);
	}
	/**
	 * pos上传状态
	 * @param arg0
	 */
	public Observable<String> serachDzcpkhxxpos(String content){
		return HttpMethodServlet.getInstance().serachDzcpkhxxpos(content);
	}
	/**
	 * 上传后本地更新状态
	 * @param arg0
	 */
	public void UpdateZtByDzpPosBySql(String arg0){
		List<SynchronizeBean> beans = GsonUtil.getSynchronizeBean(arg0);
		DaoSession session = BankApplication.getDaoSession();
		for (SynchronizeBean bean : beans){
			String sql = "update DZ_CPKH_XX_DJ_POS set xgzt = 1 where app_Uuid = '"+bean.getWybs()+"'";
			session.getDatabase().execSQL(sql);
		}
	}
	public Observable<String> getWcjPosListData(String content){
		return HttpMethodServlet.getInstance().getWcjPosListData(content);
	}
	//pos个人申报记录
	public Observable<String> getPosGeRenListData(String content){
		return HttpMethodServlet.getInstance().getPosGeRenListData(content);
	}
	//pos列表未成交查询
	public Observable<String> getSearchPos(String content){
				return HttpMethodServlet.getInstance().getSearchPos(content);
			}
	//pos列表未成交下拉显示
	public Observable<String> getDzcpPosUpListData(String content){
				return HttpMethodServlet.getInstance().getDzcpPosUpListData(content);
			}
			
			//etc列表未成交上拉显示
	 public Observable<String> getDzcpPosOfListData(String content){
				return HttpMethodServlet.getInstance().getDzcpPosOfListData(content);
			}
	 //pos个人申报记录
		public Observable<String> getDeleteDzcpPos(String content){
					return HttpMethodServlet.getInstance().getDeleteDzcpPos(content);
				}
		//pos个人下拉申报记录
		public Observable<String> getUpDzcpPosGeRenJl(String content){
					return HttpMethodServlet.getInstance().getUpDzcpPosGeRenJl(content);
				}
		//pos个人下拉申报记录
		public Observable<String> getOffDzcpPosGeRenJl(String content){
							return HttpMethodServlet.getInstance().getOffDzcpPosGeRenJl(content);
						}
						//pos个人下拉申报记录
		public Observable<String> getSearchDzcpPosGeRenJl(String content){
				return HttpMethodServlet.getInstance().getSearchDzcpPosGeRenJl(content);
								}
		public Observable<String> comparePosZjh(String content){
			return HttpMethodServlet.getInstance().comparePosZjh(content);
							}
		public Observable<String> searchDzcpPos(String content){
								return HttpMethodServlet.getInstance().searchDzcpPos(content);
												}
		public Observable<String> searchShaiXuanDzcpPos(String content){
			return HttpMethodServlet.getInstance().searchShaiXuanDzcpPos(content);
							}
}
